package com.itcam.interview.pro00.list;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class HashCodeAndEquals {
    public static void main(String[] args) {
        Map<Person, String> map = new HashMap<>();

        Person person1 = new Person("Cammy");
        Person person2 = new Person("Cammy");

        map.put(person1, "p1");
        System.out.println(map.get(person2));

        String s1 = new String();
        String s2 = new String();
        s1 = "Cammy";
        s2 = "Cammy";
        System.out.println(s1.equals(s2));
    }
}

class Person {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }
}
